package com.csoa.community.requests;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;

import android.util.Log;

import com.csoa.community.bean.SoapBody;
import com.csoa.community.net.DataProcessor;
import com.csoa.community.net.ErrorMsg;
import com.csoa.community.net.ReturnMsg;
import com.csoa.community.utils.ConvertUtils;
import com.csoa.community.utils.Json;

public abstract class AbstractRequest implements DataProcessor {
	protected String url;
	private String jsonContent;
	private String uiTitle;
	public boolean forceUpdate;
	public ReturnMsg retMsg;
	protected SoapBody soap;
	private String method;
	protected List<BasicNameValuePair> requestValues;
	protected Map<String, String> requestMapValues;
	protected String reqGetValues;
	public String fileName;
	public File respondFile;
	public static final int REQ_Get = 0;
	public static final int REQ_STREAM = 1;
	public static final int REQ_SOAP = 2;
	public int type;
	private String soapAciton;
	@SuppressWarnings("unused")
	private String requestUrl;

	public AbstractRequest(String url, String fileName) {
		this.url = url;
		this.fileName = fileName;
		this.type = REQ_STREAM;
	}

	
	
	public AbstractRequest(String method, List<BasicNameValuePair> requestValues) {
		if (requestValues != null) {
			String str = "";
			for (int i = 0; i < requestValues.size(); i++) {
				str = str + "(" + requestValues.get(i).getName() + ":" + requestValues.get(i).getValue() + ")  ";
			}
			Log.e("requestValues", str);
		}
		this.method = method;
		this.requestValues = requestValues;
	}
	
	public AbstractRequest(String method, List<BasicNameValuePair> requestValues,String url,String soapAciton) {
		this(method,requestValues);
		this.url = url;
		this.soapAciton = soapAciton;
	}
	
	
	public AbstractRequest(String url, Map<String, String> requestMapValues) {
		requestUrl = url;
		this.requestMapValues = requestMapValues;
	}
	
	public String getSoapAciton() {
		return soapAciton;
	}



	public void setSoapAciton(String soapAciton) {
		this.soapAciton = soapAciton;
	}



	public AbstractRequest(String method) {
		this.method = method;
	}
	
	public AbstractRequest() {
		
	}
	
	public AbstractRequest(String url, Map<String, String> reqMapValues,String fileName) {
		this.type = REQ_STREAM;
		if (reqMapValues != null) {
			List<String> refs = ConvertUtils.set2List(reqMapValues.keySet());
			StringBuilder sb = new StringBuilder();
			sb.append("?");
			for (int i = 0; i < refs.size(); i++) {
				sb.append(refs.get(i)).append("=").append(reqMapValues.get(refs.get(i)));
				if (i < refs.size() - 1) {
					sb.append("&");
				}
			}
			url += sb.toString();
		}
		this.url = url;
		this.fileName=fileName;		
	}
	
	public AbstractRequest(String url, SoapBody soap) {
		this.url = url;
		this.soap = soap;
		this.type = REQ_SOAP;
	}

	public String getUrl() {
		return url;
	}
	
	
	public SoapBody getSoap() {
		return soap;
	}


	public String getJsonContent() {
		return jsonContent;
	}

	public String getUITitle() {
		return uiTitle;
	}
	
	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}
	
	public List<BasicNameValuePair> getRequestValues() {
		return requestValues;
	}


	public void setRequestValues(List<BasicNameValuePair> requestValues) {
		this.requestValues = requestValues;
	}
	
	public Map<String, String> getRequestMapValues() {
		return requestMapValues;
	}
	
	public void setRequestMapValues(Map<String, String> requestMapValues) {
		this.requestMapValues = requestMapValues;
	}

	@Override
	public void read(String jsonString) throws ErrorMsg {
		parseGeneralInfo(jsonString);
		if (retMsg.retCode != SERVER_RET_ERROR) {
			try {
				parseRemaingInfo(jsonString);
			} catch (JSONException e) {
				throw new ErrorMsg("json.exception");
			}
		} else {
			throw new ErrorMsg(retMsg.retMsg);
		}
	}

	/**
	 * 
	 * @throws JSONException
	 */
	protected abstract void parseRemaingInfo(String json) throws ErrorMsg, JSONException;

	protected HashMap<String, Object> retValue;

	/**
	 */
	private void parseGeneralInfo(String jsonString) {
		retMsg = new ReturnMsg();
		retMsg = parseRetInfo(jsonString);
	}


	public static ReturnMsg parseRetInfo(String json) {
		ReturnMsg ret = new ReturnMsg();
		if (json != null && !"".trim().equals(json)) {
			Json rjson = new Json(json);
			ret.retMsg = rjson.getString("msg");
			ret.retCode = rjson.getInt("code");
			ret.succeed = (ret.retCode == 1);
		}
		return ret;
	}
}
